﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;



namespace School.Pages.Admission
{
    public partial class Student_Attachment : System.Web.UI.Page
    {
        long studentid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1012) == true)
                {


                    studentid = int.Parse(Request.QueryString.Get("sid").ToString());
                    TE_Student mystudent = Student.Single(studentid);
                    txtStudentName.Text = mystudent.Ar_FirstName + " " + mystudent.Ar_SecondName + " " + mystudent.Ar_ThirdNmae + " " + mystudent.Ar_FourthName;
                    txtStudentName1.Text = mystudent.Ar_FirstName + " " + mystudent.Ar_SecondName + " " + mystudent.Ar_ThirdNmae + " " + mystudent.Ar_FourthName;
                    if (!(Page.IsPostBack))
                    {
                        Fillcrls();
                        InitializeControls();
                        GridDataBind();
                    }

                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();

            SearchTable.Add("Student_Id", studentid.ToString());
            
            if (txtSearchAttachDesc.Text.Length > 0)
            {
                SearchTable.Add("AttachDescription", txtSearchAttachDesc.Text.ToString());
            }
            IEnumerable<SD_Student_Attachments> Results = StudentAttach.All(SearchTable);
            griddata.DataSource = Results;

            griddata.DataBind();

            griddata.Visible = true;
            mycount = Results.Count();
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {
        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            Fillcrls();
        }

        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {
           
        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;

        }

        #endregion


        # region "Grid Events ***********"

        //***************** This is function to handle page when Page Index Changing   ***************************
        //-**************************************************************************

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }


        //***************** This is function to handle page when Row Deleting  ***************************
        //-**************************************************************************

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                griddata.SelectedIndex = e.RowIndex;

                long CID = Int32.Parse(griddata.SelectedDataKey.Value.ToString());
                Boolean successadd = StudentAttach.Delete(CID);
                if (successadd == true)
                {
                    GridDataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "تم حذف البيانات بنجاح";

                }
               
            }
            catch (Exception)
            {
                lblMessage.Text = " لم يتم حذف البيانات بنجاح رجاء مراجعة مدير النظام";

            }

        }


        //***************** This is function to handle page when Row Data Bound  ***************************
        //-**************************************************************************
        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LB = (LinkButton)e.Row.FindControl("LinkButton1");
                LB.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل ترغب في حذف هذا المرفق ؟؟  ')");

            }
        }

        protected void griddata_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;

        }

        # endregion
        # region   "Execute commands sent to the form"

        //***************** This is function to handle page when Puser click in الغاء btn  ***************************
        //-**************************************************************************
        protected void btnCancel_Click(object sender, EventArgs e)
        {

            Fillcrls(); 
        }


        //***************** This is function to handle page when Puser click in اضافة btn  ***************************
        //-**************************************************************************

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            InitializeControls();
            lblMessageAdd.Text = "";
            lblMessage.Text = "";
            pnlAdd.Visible = true;
            pnlbrows.Visible = false;
        }

        //***************** This is function to handle page when Puser click in انهاء btn  ***************************
        //-**************************************************************************


        protected void btnend_Click(object sender, EventArgs e)
        {
            lblMessageAdd.Text = "";
            lblMessage.Text = "";
            pnlAdd.Visible = false;
            pnlbrows.Visible = true;
            GridDataBind();
        }
        //***************** This is function to handle page when Puser click in حفظ btn  ***************************
        //-**************************************************************************

        protected void btnSave_Click(object sender, EventArgs e)
        {

            SD_Student_Attachments ObjAttach = new SD_Student_Attachments() { Student_Id = studentid , Attach_Description = txtAttachDesc.Text , AttachFileName = FileUpload1.FileName };
            bool Uploaded = false ;
            Int64 xx = StudentAttach.Add(ObjAttach);
            if (xx != -1)
            {
                if (FileUpload1.HasFile)
                {
                    try
                    {
                        if (FileUpload1.PostedFile.ContentLength < 1048578)
                        {
                            if (System.IO.Directory.Exists(Server.MapPath("~/StudentAttach/") + studentid) == false)
                            {
                                System.IO.Directory.CreateDirectory(Server.MapPath("~/StudentAttach/") + studentid);
                            }
                            string filename = Path.GetFileName(FileUpload1.FileName);
                            FileUpload1.SaveAs(Server.MapPath("~/StudentAttach/") + studentid + "/" + FileUpload1.FileName);
                            Uploaded = true;
                        }
                        else
                        {
                            lblMessageAdd.Text = "حجم الملف أكبر من 2 ميجا ";
                            Uploaded = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        Uploaded = false;
                    }
                }
                if (Uploaded == true)
                {
                    lblMessageAdd.Text = "تم الحفظ بنجاح";
                    txtAttachDesc.Text = "";
                }
                else
                {
                    StudentAttach.Delete(xx);
                }
            }
            else
            {
                lblMessageAdd.Text = "لم يتم حفظ البيانات";
            }

        }


        # endregion



        # region " Search *********************"




        //***************** This is function to search by  Get All btn ***************************
        //-**************************************************************************
        protected void btnGetAll_Click(object sender, EventArgs e)
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();

            griddata.DataSource = StudentAttach.All(SearchTable);

            griddata.DataBind();

            griddata.Visible = true;
            mycount = griddata.Rows.Count;
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }
        }


        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        # endregion

        protected void btnback_Click(object sender, EventArgs e)
        {
            
        }

    }
}